package com.michael.adapter.aspect;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Slf4j
@Component
public class DoLogAspect {
    @Around(value = "methodPointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        MethodSignature sign = (MethodSignature) pjp.getSignature();
        Method method = sign.getMethod();
        Class<?> declaringClass = method.getDeclaringClass();
        String methodName = method.getName();
        try {
            Object object = pjp.proceed();
            log.info("method_invoke_success :{}.{}({})", declaringClass, methodName, JSON.toJSONString(args));
            return object;
        } catch (Throwable e) {
            log.info("method_invoke_fail :{}.{}({}) exception ", declaringClass, methodName, JSON.toJSONString(args),e);
            throw e;
        }
    }
    /**
     * 切入点入口
     */
    @Pointcut("annoPointcut() || controllerPointCut()")
    public void methodPointcut() {
    }

    /**
     * 基于注解的切点
     */
    @Pointcut(value = "@annotation(com.michael.assist.anno.DoLog)")
    public void annoPointcut() {
    }

    /**
     * controller控制层切点 - ALL
     */
    @Pointcut("execution(* com.michael.web.controller.*.*(..))")
    public void controllerPointCut() {
    }
}
